package com.xuecheng.media.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 阿里云上传工具类
 *
 * @author 七安
 */
@Component
public class VodTemplate {

    @Autowired
    private DefaultAcsClient client;

    /**
     * 获取视频上传地址和凭证
     *
     * @param title    流媒体文件的显示名称
     * @param fileName 流媒体文件的原始名称
     * @return CreateUploadVideoResponse 获取视频上传地址和凭证响应数据
     */
    public CreateUploadVideoResponse createUploadVideo(String title, String fileName) throws ClientException {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        //设置视频标题
        request.setTitle(title);
        //设置视频源文件名
        request.setFileName(fileName);
        return client.getAcsResponse(request);
    }

    /**
     * 刷新视频上传凭证
     *
     * @return RefreshUploadVideoResponse 刷新视频上传凭证响应数据
     */
    public RefreshUploadVideoResponse refreshUploadVideo(String videoId) throws ClientException {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 获得媒资播放请求对象
     *
     * @param videoId 视频id
     */
    public GetPlayInfoResponse getPlayInfo(String videoId) throws ClientException {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 删除视频
     *
     * @param videoIds 流媒体文件的fileId值
     * @return DeleteVideoResponse 删除视频响应数据
     * @throws Exception
     */
    public void deleteVideo(String... videoIds) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        if (ObjectUtils.isEmpty(videoIds)) {
            return;
        }
        //支持传入多个视频ID，多个用逗号分隔
        String videoIdsStr = String.join(",", videoIds);
        request.setVideoIds(videoIdsStr);
        client.getAcsResponse(request);
    }
}
